import config from './config.js';
// const AgentDetection = uni.requireNativePlugin('Karma617-AgentDetection');
// 西瓜猪淘客全网首发私有化部署,数据放在自己手里，参考：https://www.xiguazhu.net/
	// QQ群:458190194    QQ212156620    微信 jiuge8631
	
	/**
	 * 1.允许用于个人学习，切勿商业使用或其他非法用途,产生的法律责任和我们无关,由使用者自行承担;

2.如果商用必须保留版权信息，请自觉遵守;

3.禁止将本项目的代码和资源进行任何形式的出售，产生的一切任何后果责任由侵权者自负。

4.该项目基于GPL-3.0协议开源,请遵守开源协议,即便是在国内
	 */
	
var requestheader;
var TOKEN;
var request = {}
var mobileInfo;

uni.getSystemInfo({
	success(res) {
		mobileInfo = res.platform
	}
})

request.post = function(url, data, requestheader, showlaoding, complete) {
	
	requestheader = requestheader || "application/x-www-form-urlencoded";
	
	try {
		const value = uni.getStorageSync('token');
		if (value) {
			TOKEN = value
		} else {
			TOKEN = ''
		}
	} catch (e) {
		TOKEN = ''
	}
	return new Promise((succ, error) => {
		uni.request({
			url: config.httpUrl + url,
			data: data,
			method: "POST",
			header: {
				"content-type": requestheader,
				"token": TOKEN,
				"mobileInfo": mobileInfo,
				"appkey":config.appkey
			},
			success: function(result) {
				if (typeof succ == "function") {
					if (result.data.code == 0) {
						succ(result.data)
					} else if (result.data.code == 401) {
						uni.removeStorage({
							key: 'token'
						})
						error(result.data)
					} else {
						error(result.data)
						uni.showToast({
							title: result.data.message,
							icon: 'none',
							position: 'bottom',
							duration: 3000
						});
					}
				}
			},
			fail: function(e) {
				console.log(e);
				if (typeof error == "function") {
					error(e)
				}
			},
			complete: function(c) {
				var endTime = new Date();
				if (typeof complete == "function") {
					complete(c);
				}
			}
		})
	})
}


request.get = function(url, data, showlaoding) {
	requestheader = requestheader || "application/x-www-form-urlencoded";
	try {
		const value = uni.getStorageSync('token');
		if (value) {
			TOKEN = value
		} else {
			TOKEN = ''
		}
	} catch (e) {
		TOKEN = ''
	}
	
	return new Promise((succ, error) => {
		uni.request({
			url: config.httpUrl + url,
			data: data,
			method: "GET",
			header: {
				"content-type": requestheader,
				"token": TOKEN,
				"mobileInfo": mobileInfo,
				"appkey":config.appkey
			},
			success: function(result) {
				uni.hideLoading()
				if (typeof succ == "function") {
					if (result.data.code == 0) {
						succ(result.data)
					} else if (result.data.code == 401) {
						uni.removeStorage({
							key: 'token'
						})
						error(result.data)
					} else {
						error(result.data)
						uni.showToast({
							title: result.data.message,
							icon: 'none',
							position: 'bottom',
							duration: 3000
						});
					}
				}
			},
			fail: function(e) {
				uni.hideLoading()
				if (typeof error == "function") {
					error(e)
				}
			},
		})
	})
}

request.upload = function(url, file, showlaoding) {
	requestheader = requestheader || "application/x-www-form-urlencoded";
	if (showlaoding) {
		uni.showLoading({
			title: ''
		})
	}
	try {
		const value = uni.getStorageSync('token');
		if (value) {
			TOKEN = value
		} else {
			TOKEN = ''
		}
	} catch (e) {
		TOKEN = ''
	}
	return new Promise((succ, error) => {
		uni.uploadFile({
			url: config.httpUrl + url,
			filePath: file,
			name: 'file',
			formData: {
				'token': TOKEN,
				"platformType":"2"
			},
			success: (uploadFileRes) => {
				uni.hideLoading()
				succ(JSON.parse(uploadFileRes.data))
			},
			fail: function(e) {
				uni.hideLoading()
				if (typeof error == "function") {
					error(e)
				}
			},
		});
	})
}

export default request
